var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", "core/Base.class", "core/Functions.module"], function (require, exports, Base, Functions) {
    "use strict";
    var LoadManager = (function (_super) {
        __extends(LoadManager, _super);
        function LoadManager() {
            var _this = _super.apply(this, arguments) || this;
            _this.countForDependTree = 0;
            return _this;
        }
        LoadManager.prototype.startup = function () {
        };
        LoadManager.prototype.load = function (config) {
            if (!config) {
                this.bug("未传入Config!");
            }
            this.startWidgets = [];
            this.dependTree = this.analysisDependTree(config);
            this.loadModules(this.dependTree[this.countForDependTree]);
        };
        LoadManager.prototype.analysisDependTree = function (config) {
            if (!config) {
                return;
            }
            var result = new Array();
            var rest = config;
            result.push([{ name: undefined }, { name: "" }]);
            for (var level = 1;; level++) {
                var a = new Array();
                var b = new Array();
                var founded = false;
                for (var i = 0, j = rest.length; i < j; i++) {
                    founded = false;
                    for (var n = 0, m = result[level - 1].length; n < m; n++) {
                        if (result[level - 1][n].name === rest[i].depend) {
                            a.push(rest[i]);
                            founded = true;
                            break;
                        }
                    }
                    if (founded === false) {
                        b.push(rest[i]);
                    }
                }
                if (a.length === 0) {
                    result.push(b);
                    return result.slice(1);
                }
                if (a.length === rest.length) {
                    result.push(a);
                    return result.slice(1);
                }
                result.push(a);
                rest = b;
            }
        };
        LoadManager.prototype.loadModules = function (currentWidgetsConfig) {
            if (!currentWidgetsConfig) {
                return;
            }
            var that = this;
            that.taskManager = new Functions.CountToRun(function () {
                that.countForDependTree++;
                that.loadModules(that.dependTree[that.countForDependTree]);
            }, that);
            that.taskManager.setCount(currentWidgetsConfig.length);
            var widgetUrls = that.getWidgetsUrl(currentWidgetsConfig);
            require({}, widgetUrls, function () {
                for (var i = 0, j = arguments.length; i < j; i++) {
                    var currentWidgetCongfigPath = Functions.concatUrl(currentWidgetsConfig[i].url, currentWidgetsConfig[i].configPath);
                    var currentWidgetTemplatePath = Functions.concatUrl(currentWidgetsConfig[i].url, currentWidgetsConfig[i].templatePath);
                  var widget =  new (arguments[i])({
                        widgetPath: currentWidgetsConfig[i].url,
                        configPath: currentWidgetsConfig[i].configPath ?
                            currentWidgetCongfigPath : undefined,
                        templatePath: currentWidgetsConfig[i].templatePath ?
                            currentWidgetTemplatePath : undefined,
                        readyCallback: that.taskManager.minus.bind(that.taskManager)
                  });
                  that.startWidgets[currentWidgetsConfig[i].name] = widget;
                }
            });
        };
        LoadManager.prototype.getWidget = function (widgetID) {
            var widget = this.startWidgets[widgetID];
            return widget;
        };
        LoadManager.prototype.getWidgetsUrl = function (config) {
            var widgetsUrl;
            if (_.isArray(config) === true) {
                widgetsUrl = _.map(config, function (widget) {
                    return Functions.concatUrl(widget.url, widget.main);
                });
            }
            return widgetsUrl;
        };

        return LoadManager;
    }(Base));
    return LoadManager;
});
